home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / asyam.zip / ASYIO.C next >
C/C++ Source or Header  |  1993-06-24  |  4KB  |  163 lines

  1. #include <dos.h>
  2.  
  3. #include "async.h"
  4.  
  5. extern  int UART_ports[];
  6. extern  struct async_portS async_port[4];
  7.  
  8. int     async_putch_timeout(int comport, char c, long timeout)
  9. {
  10.     long    to;
  11.  
  12.     to = timeout;
  13.     while (async_port[comport].txbuflength>=TXBUFSIZE) {
  14.         if (timeout) {
  15.             to--;
  16.             delay(1);
  17.  
  18.             if (to == 0) {
  19.                 return(0);
  20.             }
  21.         }
  22.     }
  23.  
  24.     async_port[comport].txbuf[async_port[comport].txhead] = c;
  25.  
  26.     async_port[comport].txhead++;
  27.     async_port[comport].txbuflength++;
  28.     if (async_port[comport].txhead == TXBUFSIZE) {
  29.         async_port[comport].txhead = 0;
  30.     }
  31.  
  32.     outportb(UART_ports[comport]+IER, 0x03);
  33.  
  34.     return (1);
  35. }
  36.  
  37. void    async_putch(int comport, char c)
  38. {
  39.     async_putch_timeout(comport, c, 0L);
  40.  
  41.     outportb(UART_ports[comport]+IER, 0x03);
  42. }
  43.  
  44. void    async_puts(int comport, char *s)
  45. {
  46.     while (*s) {
  47.         async_putch(comport, *s++);
  48.     }
  49. }
  50.  
  51. int     async_putblock_timeout(int comport, const char *block, int size, long timeout)
  52. {
  53.     long    to;
  54.  
  55.     while (size--) {
  56.         to = timeout;
  57.  
  58.         // Wait for room in the TX buffer
  59.         while (async_port[comport].txbuflength>=TXBUFSIZE) {
  60.             if (timeout) {
  61.                 to--;
  62.                 delay(1);
  63.  
  64.                 if (to == 0) {
  65.                     return(0);
  66.                 }
  67.             }
  68.         }
  69.  
  70.         // Go ahead and put next character from block into TX buffer
  71.         async_port[comport].txbuf[async_port[comport].txhead] = *block++;
  72.  
  73.         // Increment buffer pointers
  74.         async_port[comport].txhead++;
  75.         async_port[comport].txbuflength++;
  76.         if (async_port[comport].txhead == TXBUFSIZE) {
  77.             async_port[comport].txhead = 0;
  78.         }
  79.     }
  80.  
  81.     outportb(UART_ports[comport]+IER, 0x03);
  82.  
  83.     return (1);
  84. }
  85.  
  86. void    async_putblock(int comport, const char *block, int size)
  87. {
  88.     async_putblock_timeout(comport, block, size, 0L);
  89. }
  90.  
  91. int     async_ready(int comport)
  92. {
  93.     if (async_port[comport].rxbuflength) {
  94.         return (1);
  95.     } else {
  96.         return (0);
  97.     }
  98. }
  99.  
  100. char    async_getch(int comport)
  101. {
  102.     char    c;
  103.  
  104.     if (async_port[comport].rxbuflength) {
  105.         c = async_port[comport].rxbuf[async_port[comport].rxtail];
  106.  
  107.         async_port[comport].rxtail++;
  108.         async_port[comport].rxbuflength--;
  109.         if (async_port[comport].rxtail == RXBUFSIZE) {
  110.             async_port[comport].rxtail = 0;
  111.         }
  112.  
  113.         return(c);
  114.     }
  115.  
  116.     return (0);
  117. }
  118.  
  119. int     async_getblock(int comport, char *block, int size)
  120. {
  121.     int     snatched = 0;
  122.  
  123.     while (size && async_port[comport].rxbuflength) {
  124.         block[snatched] = async_port[comport].rxbuf[async_port[comport].rxtail];
  125.  
  126.         size--;
  127.         async_port[comport].rxbuflength--;
  128.         snatched++;
  129.         async_port[comport].rxtail++;
  130.         if (async_port[comport].rxtail == RXBUFSIZE) {
  131.             async_port[comport].rxtail = 0;
  132.         }
  133.     }
  134.  
  135.     return (snatched);
  136. }
  137.  
  138. char    async_peek(int comport)
  139. {
  140.     char    c;
  141.  
  142.     if (async_port[comport].rxhead != async_port[comport].rxtail) {
  143.         c = async_port[comport].rxbuf[async_port[comport].rxtail];
  144.         return (c);
  145.     }
  146.  
  147.     return (0);
  148. }
  149.  
  150. void    async_flushrx(int comport)
  151. {
  152.     async_port[comport].rxhead = 0;
  153.     async_port[comport].rxtail = 0;
  154.     async_port[comport].rxbuflength = 0;
  155. }
  156.  
  157. void    async_flushtx(int comport)
  158. {
  159.     async_port[comport].txhead = 0;
  160.     async_port[comport].txtail = 0;
  161.     async_port[comport].txbuflength = 0;
  162. }
  163.